using System;
using Tayx.Graphy.Utils.NumString;
using UnityEngine;
using WorldMapBase.Tile;
using Yoozoo.Gta.Common;

namespace UnityGameFramework.Runtime
{
    public sealed partial class DebuggerComponent : GameFrameworkComponent<DebuggerComponent>
    {
        private sealed class TextureStreamingWindow : ScrollableDebuggerWindowBase
        {
            protected override void OnDrawScrollableWindow()
            {
                GUILayout.Label("<b>TextureStreaming Settings</b>");
                GUILayout.BeginVertical("box");
                {
                    OnDrawHorizontalToggle("是否开启TextureStreaming: ", QualitySettings.streamingMipmapsActive,"", (b) =>
                    {
                        QualitySettings.streamingMipmapsActive = b;
                        
                    });
                    OnDrawHorizontalToggle("AddAllCameras: ", QualitySettings.streamingMipmapsAddAllCameras,"", (b) =>
                        {
                            QualitySettings.streamingMipmapsAddAllCameras = b;
                        });
                    
                    OnDrawHorizontalToggle("DiscardUnusedMips: ", Texture.streamingTextureDiscardUnusedMips,"", (b) =>
                    {
                        Texture.streamingTextureDiscardUnusedMips = b;
                    });
                    
                    OnDrawHorizontalToggle("ForceLoadAll: ", Texture.streamingTextureForceLoadAll,"", (b) =>
                    {
                        Texture.streamingTextureForceLoadAll = b;
                    });
                    
                    
                    var memoryBudget = QualitySettings.streamingMipmapsMemoryBudget;
                    OnDrawHorizontalTextField("Budget size: ", memoryBudget,(f =>
                    {
                        QualitySettings.streamingMipmapsMemoryBudget = f;
                    }));
                    
                    var MaxLevelReduction = QualitySettings.streamingMipmapsMaxLevelReduction;
                    OnDrawHorizontalSlider("MaxLevelReduction: ", MaxLevelReduction,0,7,(f =>
                    {
                        QualitySettings.streamingMipmapsMaxLevelReduction = f.ToInt();
                    }));
                    
                    var renderersPerFrame = QualitySettings.streamingMipmapsRenderersPerFrame;
                    OnDrawHorizontalTextField("renderersPerFrame: ", renderersPerFrame,(f =>
                    {
#if UNITY_EDITOR
                        QualitySettings.streamingMipmapsRenderersPerFrame = f.ToInt();
#endif
                        
                    }));
                    
                    
                    var maxFileIORequests = QualitySettings.streamingMipmapsMaxFileIORequests;
                    OnDrawHorizontalTextField("MaxFileIORequests: ", maxFileIORequests,(f =>
                    {
                        QualitySettings.streamingMipmapsMaxFileIORequests = f.ToInt();
                    }));

                }
                GUILayout.Label("<b>texture memory</b>");
                DrawItem("Discard unused mips :", Texture.streamingTextureDiscardUnusedMips.ToString());
                DrawItem("Desired texture memory:", (Texture.desiredTextureMemory/1048576f).ToString());
                DrawItem("Target texture memory:", (Texture.targetTextureMemory/1048576f).ToString());
                DrawItem("Hightest Desired  texture memory:", ((Texture.totalTextureMemory-Texture.nonStreamingTextureMemory)/1048576f).ToString());
                DrawItem("Non Streaming  texture memory:", (Texture.nonStreamingTextureMemory/1048576f).ToString());
                DrawItem("Total texture memory:", (Texture.totalTextureMemory/1048576f).ToString());
                
                GUILayout.Label("<b>信息统计</b>");
                DrawItem("Memory Budget利用率:", (Texture.desiredTextureMemory/1048576f/QualitySettings.streamingMipmapsMemoryBudget*100).ToString());
                
                DrawItem("streamingMipmapUploadCount:", (Texture.streamingMipmapUploadCount).ToString());
                DrawItem("streamingRendererCount:", (Texture.streamingRendererCount).ToString());
                DrawItem("streamingTextureCount:", (Texture.streamingTextureCount).ToString());
                DrawItem("nonStreamingTextureCount:", (Texture.nonStreamingTextureCount).ToString());
                DrawItem("streamingTexturePendingLoadCount:", (Texture.streamingTexturePendingLoadCount).ToString());
                DrawItem("streamingTextureLoadingCount:", (Texture.streamingTextureLoadingCount).ToString());
                
                GUILayout.Label("<b>内存节省</b>");
                
                DrawItem("Memory saving:", ((Texture.totalTextureMemory-Texture.desiredTextureMemory)/1048576f).ToString());
                DrawItem("降低百分比 :", ((Texture.totalTextureMemory-Texture.desiredTextureMemory)/Texture.totalTextureMemory*100).ToString());
                
                GUILayout.EndVertical();

            }
            private void OnDrawHorizontalSlider(string label, ref float value, float min, float max, Action<float> modifyCallback)
            {
                GUILayout.BeginHorizontal();
                {
                    float temp = value;
                    label = string.Format("{0}:{1}", label, value);
                    GUILayout.Label(label, GUILayout.Width(150f));
                    if (GUILayout.RepeatButton("-", GUILayout.Width(30f)))
                    {
                        temp--;
                    }
                    temp = GUILayout.HorizontalSlider(temp, min, max,new GUILayoutOption[]{GUILayout.MaxWidth(300f)});
                    if (GUILayout.RepeatButton("+", GUILayout.Width(30f)))
                    {
                        temp++;
                    }
                    temp = Mathf.Clamp(temp, min, max);
                    if (temp != value)
                    {
                        temp = (float)Math.Round(temp,2);
                        if (modifyCallback!=null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }

            private void OnDrawHorizontalSlider(string label, float value, float min, float max, Action<float> modifyCallback)
            {
                OnDrawHorizontalSlider(label,ref value,min,max, modifyCallback);
            }
            private void OnDrawHorizontalToggle(string label, bool value, string toogleStr, Action<bool>
                modifyCallback = null)
            {
                GUILayout.BeginHorizontal();
                {
                    bool temp = value;
                    label = string.Format("{0}:{1}", label, value);
                    GUILayout.Label(label, GUILayout.Width(200f));
                    temp = GUILayout.Toggle(value, toogleStr, GUILayout.Width(200f));
                    if (temp != value)
                    {
                        if (modifyCallback != null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }
            
            private void OnDrawHorizontalTextField(string label, float value, Action<float> modifyCallback = null)
            {
                OnDrawHorizontalTextField(label, ref value, modifyCallback);
            }
                
            private void OnDrawHorizontalTextField(string label,ref float value, Action<float> modifyCallback = null)
            {

                GUILayout.BeginHorizontal();
                {
                    float temp = value;
                    GUILayout.Label(label, GUILayout.Width(200f));
                    string tempStr = GUILayout.TextField(Convert.ToString(value),new GUILayoutOption[] { GUILayout.MaxWidth(200f) });
                    tempStr = string.IsNullOrEmpty(tempStr) ? "0" : tempStr;
                    temp = (float)Convert.ToDouble(tempStr);
                    if (temp != value)
                    {
                        if (modifyCallback != null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }
        }
    }
}